using System;
using System.Collections.Generic;
using System.Threading;

namespace Assets.Scripts.GDK
{
	public class gdk_threadDownLoadList
	{
		private int threadLen = 5;

		private static gdk_threadDownLoadList _instance;

		public static gdk_threadDownLoadList Instance
		{
			get
			{
				if (_instance == null)
				{
					_instance = new gdk_threadDownLoadList();
				}
				return _instance;
			}
			set
			{
				_instance = value;
			}
		}

		public void DownLoad(List<string> _URLlist, Action<string> itemStartCallBack)
		{
			List<string> URLlist = gdk_utils.copyList(_URLlist);
			Thread[] array = new Thread[threadLen];
			for (int i = 0; i < threadLen; i++)
			{
				Thread thread = array[i];
				ThreadStart start = delegate
				{
					if (!checkedAll(URLlist))
					{
						DownLoadFun(URLlist, itemStartCallBack, delegate(bool end1)
						{
							if (!end1)
							{
								DownLoadFun(URLlist, itemStartCallBack, delegate(bool end2)
								{
									if (!end2)
									{
									}
								});
							}
						});
					}
					else
					{
						thread.Abort();
					}
				};
				thread = new Thread(start);
				thread.Name = $"线程{i}:";
				thread.Start();
			}
		}

		private string nextURL(List<string> URLlist)
		{
			string result = URLlist[0];
			URLlist.RemoveAt(0);
			return result;
		}

		private bool checkedAll(List<string> URLlist)
		{
			return URLlist.Count == 0;
		}

		private void DownLoadFun(List<string> URLlist, Action<string> itemStartCallBack, Action<bool> DownLoadCallback)
		{
			if (!checkedAll(URLlist))
			{
				string uRL = nextURL(URLlist);
				DownLoadAction(uRL, itemStartCallBack);
			}
			DownLoadCallback(!checkedAll(URLlist));
		}

		private void DownLoadAction(string URL, Action<string> itemStartCallBack)
		{
			itemStartCallBack(URL);
		}
	}
}
